package com.gitee.secretopen.plugin.model;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import com.gitee.secretopen.plugin.SecretPluginFactory;
import com.gitee.secretopen.plugin.config.SecretPluginConfig;
import com.gitee.secretopen.plugin.observer.Observer;
import com.gitee.secretopen.plugin.util.JsonUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 　@description: 观察者，接收插件信息实体类更新消息
 * 　@author secret
 * 　@date 2021/2/3 14:32
 *
 */
public class PluginModelObserver implements Observer {

    SecretPluginConfig config=SecretPluginConfig.getInstance();

    @Override
    public void update(Object message) {
        outPluginJson();
    }

    private void outPluginJson(){
        //获取插件信息列表
        List<PluginModel> pluginModelList=SecretPluginFactory.getPluginModelList();
        //获取json配置文件
        String jsonPath=config.getPluginJsonPath();
        File pluginJsonFile = new File(jsonPath);
        //覆盖插件文件
        String newPluginJsonStr= JsonUtil.objectToJsonFormat(pluginModelList);
        FileWriter fileWriter=new FileWriter(pluginJsonFile);
        fileWriter.write(newPluginJsonStr);
    }
}
